﻿using System;
using System.Data;
using TopBuilds.Database;

namespace TopBuilds.Restaurant.Data
{
    public class PrintTemplateObject
    {
        public static PrintTemplate[] GetPrintTemplates()
        {
            return Query(null);
        }

        public static PrintTemplate GetPrintTemplate(int id)
        {
            PrintTemplate printTemplate = new PrintTemplate();

            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(PrintTemplateDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = PrintTemplateDesc.TableName;
            string sql = builder.QueryCommandLine;

            DataTable printTemplates = DatabaseObject.Instance.ExecuteQuerySQL(sql);

            printTemplate.Id               = Convert.ToInt32(printTemplates.Rows[0][PrintTemplateDesc.Id]);
            printTemplate.Name             = Convert.ToString(printTemplates.Rows[0][PrintTemplateDesc.Name]);
            printTemplate.version          = Convert.ToInt32(printTemplates.Rows[0][PrintTemplateDesc.version]);
            printTemplate.width            = Convert.ToInt32(printTemplates.Rows[0][PrintTemplateDesc.width]);
            printTemplate.HeadHeight       = Convert.ToInt32(printTemplates.Rows[0][PrintTemplateDesc.HeadHeight]);
            printTemplate.BodyHeight       = Convert.ToInt32(printTemplates.Rows[0][PrintTemplateDesc.BodyHeight]);
            printTemplate.FootHeight       = Convert.ToInt32(printTemplates.Rows[0][PrintTemplateDesc.FootHeight]);

            return printTemplate;
        }

        public static PrintTemplate GetPrintTemplate(string name)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(PrintTemplateDesc.Name, ConditionExpress.EQUALS, name);

            PrintTemplate[] set = Query(builder.ConditionString);

            if (set != null && set.Length > 0)
            {
                return set[0];
            }

            return null;
        }

        public static PrintTemplate[] Query(string condition)
        {


            // query factories from database
            string sql = "select * from " + PrintTemplateDesc.TableName;
            if (condition != null && condition.Length > 0)
            {
                sql += " where " + condition;
            }
            DataTable table = DatabaseObject.Instance.ExecuteQuerySQL(sql);


            // rebuild the data
            if (table != null && table.Rows.Count > 0)
            {
                PrintTemplate[] printTemplates = new PrintTemplate[table.Rows.Count];

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    PrintTemplate printTemplate = new PrintTemplate();

                    printTemplate.Id                    = Convert.ToInt32(table.Rows[i][PrintTemplateDesc.Id]);
                    printTemplate.Name                  = Convert.ToString(table.Rows[i][PrintTemplateDesc.Name]);
                    printTemplate.version               = Convert.ToInt32(table.Rows[i][PrintTemplateDesc.version]);
                    printTemplate.width                 = Convert.ToInt32(table.Rows[i][PrintTemplateDesc.width]);
                    printTemplate.HeadHeight            = Convert.ToInt32(table.Rows[i][PrintTemplateDesc.HeadHeight]);
                    printTemplate.BodyHeight            = Convert.ToInt32(table.Rows[i][PrintTemplateDesc.BodyHeight]);
                    printTemplate.FootHeight            = Convert.ToInt32(table.Rows[i][PrintTemplateDesc.FootHeight]);

                    printTemplates.SetValue(printTemplate, i);
                }

                return printTemplates;
            }
            return null;
        }

        public static Int32 Add(PrintTemplate printTemplate)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.InsertAdd(PrintTemplateDesc.Name,          printTemplate.Name);
            builder.InsertAdd(PrintTemplateDesc.version,       printTemplate.version);
            builder.InsertAdd(PrintTemplateDesc.width,         printTemplate.width);
            builder.InsertAdd(PrintTemplateDesc.HeadHeight,    printTemplate.HeadHeight);
            builder.InsertAdd(PrintTemplateDesc.BodyHeight,    printTemplate.BodyHeight);
            builder.InsertAdd(PrintTemplateDesc.FootHeight,    printTemplate.FootHeight);
            builder.QueryTableName = PrintTemplateDesc.TableName;

            string sql = builder.InsertCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }

        public static bool Delete(Int32 id)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(PrintTemplateDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = PrintTemplateDesc.TableName;
            string sql = builder.DeleteCommandLine;

            return (DatabaseObject.Instance.ExecuteNonQuerySQL(sql) != -1);
        }

        public static Int32 Update(PrintTemplate printTemplate)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.UpdateAdd(PrintTemplateDesc.Name,                printTemplate.Name);
            builder.UpdateAdd(PrintTemplateDesc.version,             printTemplate.version);
            builder.UpdateAdd(PrintTemplateDesc.width,               printTemplate.width);
            builder.UpdateAdd(PrintTemplateDesc.HeadHeight,          printTemplate.HeadHeight);
            builder.UpdateAdd(PrintTemplateDesc.BodyHeight,          printTemplate.BodyHeight);
            builder.UpdateAdd(PrintTemplateDesc.FootHeight,          printTemplate.FootHeight);
            builder.ConditionAdd(PrintTemplateDesc.Id, ConditionExpress.EQUALS, printTemplate.Id);
            builder.QueryTableName = PrintTemplateDesc.TableName;

            string sql = builder.UpdateCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }
    }
}
